import {myFetch} from '@/assets/js/utils'
import pingpp from 'pingpp-js'

export default{
    // 购物车结算
    async settleCartGoods (goods_id) {
        const data = await myFetch({
            name: 'shopping.sys.settle.account',
            user_id: 0,
            goods_id
        })
        return data
    },
    // 获取邮费
    async getPostage ({selectedGoodsId, addressId}) {
        const data = await myFetch({
            name: 'shopping.sys.get.postage',
            goods_id: selectedGoodsId,
            address_id: addressId,
            user_id: 0,
            scores_used: 0
        })
        return data
    },
    // 提交订单
    async submitOrder ({address_id, goods_id, user_id, note,type}) {
        const data = await myFetch({
            name: 'shopping.sys.submit.order',
            address_id,
            goods_id,
            is_selected: 0,
            note,
            type,
            user_id,
            idcard: '',
            real_name: '',
        })
        return data
    },
    // 提交订单号 获取charge
    async getCharge ({type, orderFormLogId, orderId, totalPrice, userId}) {
        // type为1时是刚结算产品 type为2时是付待付款的产品 4是虚拟产品
        let description
        switch (type) {
        case 1:
            description = {
                type: 1,
                add_scores: 0,
                reduce_scores: 0,
                user_id: userId,
                order_form_log_id: orderFormLogId
            }
            break
        case 2:
            description = {
                type: 2,
                add_scores: 0,
                reduce_scores: 0,
                user_id: userId,
                order_id: orderId
            }
            break
        case 4:
            description = {
                type: 4,
                add_scores: 0,
                reduce_scores: 0,
                user_id: userId,
                order_form_log_id: orderId,
                order_id: orderId
            }
            break
        }
        const data = await myFetch({
            name: 'shopping.sys.goods.pay',
            channel: 'wx_pub',
            amount: totalPrice * 100,
            order_no: Math.ceil(Math.random() * 100) + Date.parse(new Date()),
            description: JSON.stringify(description)
        }, {url2: type === 4})
        return data
    },
    // 调用pingpp支付
    WxPay (charge) {
        return new Promise((resolve, reject) => {
            pingpp.createPayment(charge, (result, err) => {
                // alert('result:' + JSON.stringify(result) + 'err:' + JSON.stringify(err));
                if (result === 'success') resolve()
                else reject(err)
            })
        })
    }
}
